$(document).ready(function() {
	$('.table-sort').dataTable(pageStting(baseURL));
	$('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	$(window).resize(function(){
		$(".table-sort").css('width','100%');
	})
});

doSearch = function(){
	   var seachValue={};
	   $('.table-sort').DataTable().search(seachValue).draw();
}	
//从服务器分批取数据
pageStting =  function(baseURL){
		var optsortable = {
					"bPaginate":true,
					"bProcessing":true,
					"stateSave":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/organization/list'+"?refever="+Math.random(),
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
					"aoColumnDefs" : [
										{
											"render" : function(data, type,full) {
												var checkBox = '<input type="checkbox" id="SelectedOrgId" name="SelectedOrgId"  class="btn btn-danger radius" value="' + full[0]+ '"/>';
												/*if(orgId == (full[0]+"")){
													checkBox = '<input type="checkbox" id="SelectedOrgId" name="SelectedOrgId" disabled=true  class="btn btn-danger radius" value="' + full[0]+ '"/>';
												}*/
												return checkBox;
											},
										 	"targets" : 0
										},
										{
											"render" : function(data, type,full) {
													var edit_herf ="<div class='text-c handlePointer'><img title='编辑组织' alt='编辑组织' src='"+baseURL+"/images/admin/cp_edite.png' onclick='editSchoolDetail("+full[0]+")' />"
					    							var trach_herf="<img title='删除' alt='删除' src='"+baseURL+"/images/admin/cp_delete.png' onclick=\"return deleteData(null,'"+full[0]+"')\" /></div>"
					                            	return edit_herf+trach_herf;
											},
											"targets" : 5
										} ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#serach_code").val())!=''){
								searchConditions.name=$.trim($("#serach_code").val());
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							$.ajax({
								url : sSource,
								type : "get",
								dataType : "json",
								data : aoData,
								timeout : 30000,
								contentType : "application/json; charset=utf-8",
								success : function(result) {
									if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
									
									if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										//json重构数组对象 
										$.each(data,function(i,val){
											aaData[i]=[];
											aaData[i].push(val.id);
											aaData[i].push(val.name==null?'/':val.name);
											aaData[i].push(val.code==null?'/':val.code);
											aaData[i].push(val.address==null?'/':val.address);
											aaData[i].push(val.phone==null?'/':val.phone);
											aaData[i].push(val.null_n);
										});
							            fnCallback(dataResult);   
						          }
								  }    
						     });
					}
				}	
		return optsortable;
}
showOrganizationRole = function (){
   var ids=[];
	var objs = $('input[name="SelectedOrgId"]:checked');
	$.each(objs,function(index,obj){
		var value = $(this).val();
		ids[index] = value;
	});
	 
	if(ids.length==0){
		layer.msg("请选择一个组织！");
		return;
	}
	if(ids.length>1){
		layer.msg("请只选中一个组织！");
		return;
	}
	layer_show('750','','管理角色',baseURL+'/api/organization/page/grantRole/'+ids[0]);
}

deleteData = function(selName, id) {
	var ids = "";
	if (selName == null) {
		ids = id;
	} else {
		var objs = $('input[name=' + selName + ']');
		$.each(objs, function(i, val) {
			if (objs[i].checked) {
				if (objs[i].value != "") {
					if (ids == "") {
						ids = objs[i].value;
					} else {
						ids = ids + "," + objs[i].value;
					}
				}
			}
		});
	}
	if (ids == "") {
		layer.msg("请选择删除项！");
		return false;
	}
	var data = {};
	data.ids = ids;
	layer.confirm('您确定要提交表单吗?',function(){
		$.ajax({
			type : "post",
			url : '../delete',
			dataType : "json",
			contentType : "application/json",
			data : JSON.stringify(data),
			success : function(result) {
				if (!result.flag) {
					layer.msg("删除失败！原因：" + result.message);
				} else {
					location.reload();
				}
			}
		});
	})
	return true;
}

editSchoolDetail = function(id){
	$.ajaxSetup ({
	    cache: false //设置成false将不会从浏览器缓存读取信息
	});
	location.href=baseURL+"/api/organization/page/edit/"+id+"?times="+(new Date()).getTime();
		
}
